package com.itheima.store.dao.impl;

import java.sql.SQLException;
import java.util.List;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;


import com.itheima.store.dao.DeptmanagerDao;
import com.itheima.store.dao.PostManagerDao;

import com.itheima.store.domain.Dept;
import com.itheima.store.domain.Post;
import com.itheima.store.utils.JDBCUtils;
/**
 * 分类的DAO的实现类
 * @author admin
 *
 */
public class PostManagerDaoImpl implements PostManagerDao {

	@Override
	public List<Post> findAllPost() throws SQLException {
		QueryRunner queryRunner = new QueryRunner(JDBCUtils.getDataSource());
		String sql = "select * from post";
		List<Post> list = queryRunner.query(sql, new BeanListHandler<Post>(Post.class));
		return list;
	}

	@Override
	public void savePost(Post post) throws SQLException {
		// TODO Auto-generated method stub
		QueryRunner queryRunner = new QueryRunner(JDBCUtils.getDataSource());
		String sql ="insert into post(post_id,post_name) values(?,?)";
		int i = queryRunner.update(sql, post.getPost_id(),post.getPost_name());
		
	}

	@Override
	public Post queryPost(String post_id) throws SQLException {
		QueryRunner queryRunner = new QueryRunner(JDBCUtils.getDataSource());
		String sql = "select * from post where post_id=?";
		Post post = queryRunner.query(sql, new BeanHandler<Post>(Post.class), post_id);
		return post;
	}

	@Override
	public void updatePost(Post post) throws SQLException {
		// TODO Auto-generated method stub
		QueryRunner queryRunner = new QueryRunner(JDBCUtils.getDataSource());
		String sql = "update post set post_name =? where post_id=?";
		queryRunner.update(sql, post.getPost_name(),post.getPost_id());
	}

	@Override
	public void deletePost(String post_id)throws SQLException {
		// TODO Auto-generated method stub
		QueryRunner queryRunner = new QueryRunner(JDBCUtils.getDataSource());
		String sql = "delete from post where post_id =?";
		queryRunner.update(sql, post_id);
		
	}

}
